<template>
  <div class="login-container">
    <div class="login-box">
      <div class="system-logo">
        <img src="@/assets/logo.png" alt="企业培训系统">
        <h1>企业培训系统</h1>
      </div>
      <el-form 
        ref="loginFormRef"
        :model="loginForm" 
        :rules="loginRules"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            size="large"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item prop="userType">
          <el-radio-group v-model="loginForm.userType" size="large">
            <el-radio-button label="student">学生登录</el-radio-button>
            <el-radio-button label="teacher">教师登录</el-radio-button>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            :loading="loading"
            size="large"
            class="login-btn"
          >
            立即登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'

const router = useRouter()
const authStore = useAuthStore()
const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: '',
  userType: 'student'
})

const loginRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在3到20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在6到20个字符', trigger: 'blur' }
  ],
  userType: [
    { required: true, message: '请选择登录类型', trigger: 'change' }
  ]
})

const handleLogin = async () => {
  try {
    loading.value = true
    await loginFormRef.value.validate()
    
    // 调用登录接口
    const response = await authStore.login(loginForm)
    
    if (!response || !response.userId) {
      throw new Error('登录失败：未能获取用户ID')
    }
    
    ElMessage.success('登录成功')
    
    // 根据用户类型跳转到不同页面
    if (loginForm.userType === 'teacher') {
      // 教师登录成功后跳转到班级列表页面
      router.push('/class')
    } else {
      // 学生登录成功后跳转到我的课程页面
      router.push('/student-courses')
    }
    
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-size: cover;
}

.login-box {
  width: 450px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

.system-logo {
  text-align: center;
  margin-bottom: 30px;
}

.system-logo img {
  height: 60px;
  margin-bottom: 15px;
}

.system-logo h1 {
  font-size: 24px;
  color: #333;
  margin: 0;
}

.login-btn {
  width: 100%;
  margin-top: 10px;
}

.el-radio-group {
  width: 100%;
  display: flex;
  justify-content: space-between;
}

.el-radio-button {
  flex: 1;
}

.el-radio-button :deep(.el-radio-button__inner) {
  width: 100%;
}
</style>